// 2014-02-01 Tyrghen
//---------------------------------------------
// Usage:
//  ["marker_zone","jip_var_conquered","The zone is under control","STR_ZoneIsLost",600,east] call edt_fnc_takeover;
//  ["marker_zone","jip_var_conquered"] call edt_fnc_takeover;
//---------------------------------------------
// Purpose:
// Creates a control zone where one side has to outnumber the other in order to take control over the zone.
// 
// Parameters:
// - Center position: Can be a marker or coordinates or an object
// Optional Parameters:
// - Conquered Variable: the name of the JIP Synchronised variable indicating whether the zone is controlled by the attackers
// - Conquered Message: the message displayed when the zone is controled by the attacking side
// - Lost Message: the message displayed when the zone is controlled by the defending side
// - Hold time: the time you need to hold (outnumber) the opposing side before the zone is under your controlNull
// - Defending side: the side this zone belongs too intially
//
// Returns:
// The object (an invisible helipad by default) around which the zone is built.
//---------------------------------------------
// Objectif:
// Crée une zone de contrôle où un camp doit surpasser en nombre l'autre camp pour en prendre le contrôle.
//
// Paramètres:
// - Position du centre: peut être un marqueur ou des coordonnées ou un objet
// Paramètres optionnels:
// - Variable de conquête: Le nom de la variable synchronisée en JIP qui indique que la zone est contrôlée par les attaquants
// - Message de conquête: Le message qui est affiché lorsque la zone est contrôllée par les attaquants
// - Message de reprise: Le message qui est affiché lorsque la zone est reprise par les défenseurs
// - Délai de conquête: Le temps que la zone doit être exclusivement sous le contrôle des attaquants avant de la considérer comme prise
// - Camp des défenseurs: Le camp auquel la zone appartient intialement
//
// Renvoie:
// L'objet (un héliport invisible par défaut) autour duquel la zone est construite.
//---------------------------------------------

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "editor\fn_takeover.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

if (!isServer) exitWith {TRACE("Not Server")};

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_type","_marker","_shared_var","_taken_msg","_lost_msg","_hold_time","_marker_size","_zone_size","_error","_obj_params","_owner_side","_text","_shape"];
DEFAULT_PARAM(_marker,0,"");
DEFAULT_PARAM(_shared_var,1,"");
_text = "";
_type = "Land_HelipadEmpty_F";
if (IS_STRING(_marker) && {IS_MARKER(_marker)}) then {
	_text = markerText _marker;
} else {
	if (IS_OBJECT(_marker)) then {
		_type = _marker;
		_text = [_type] call FFUNC(displayName);
	};
};
DEFAULT_PARAM(_taken_msg,2,ARR_2("STR_TMF_TAKENOVER",_text));
DEFAULT_PARAM(_lost_msg,3,ARR_2("STR_TMF_TAKENBACK",_text));
DEFAULT_PARAM(_hold_time,4,30);
DEFAULT_PARAM(_owner_side,5,[GVAR(side_enemy)]);
DEFAULT_PARAM(_type,6,_type);

_obj = objNull;
_is_valid = true;

if (AND_ALSO(IS_STRING(_type),(_type == ""))) then {
	_is_valid = false;
};
if (AND_ALSO(IS_STRING(_type),(_type isKindOf "Man" || _type isKindOf "LandVehicle" || _type isKindOf "Air" || _type isKindOf "Ship"))) then {
	_is_valid = false;
};
if (AND_ALSO(IS_STRING(_type),IS_STRING(_marker))) then {
	_is_valid = _is_valid && _marker != "";
};
if (AND_ALSO(IS_STRING(_type),IS_ARRAY(_marker))) then {
	_is_valid = _is_valid && count _marker < 2;
};
if (AND_ALSO(IS_OBJECT(_marker),(isNull _marker))) then {
	_is_valid = false;
};
_shared_var = _shared_var call FFUNC(getStringVar);

if (_is_valid) then {
	_obj_params = [];
	_obj_params set [TMF_VEH_PARAMS_TYPE, _type];
	_shape = [_marker] call FFUNC(toShape);
	TRACE_1("Shape",_shape);
	_obj_params set [TMF_VEH_PARAMS_POS, (_shape select 0)];

	_zone_size = 30;

	if (IS_STRING(_taken_msg)) then { _taken_msg = [_taken_msg] };
	if (IS_STRING(_lost_msg)) then { _lost_msg = [_lost_msg] };

	if (IS_STRING(_marker)) then {
		if (IS_MARKER(_marker)) then {
			_marker_size = ((markerSize _marker) select 0) max ((markerSize _marker) select 1);
			if (_marker_size > 1) then { _zone_size = _marker_size };
		};
	};
	_tmf_params = [];
	_tmf_params = [_tmf_params,"ZONE_SIZE",_zone_size] call FFUNC(hashSet);
	_tmf_params = [_tmf_params,"TAKEN_MSG",_taken_msg] call FFUNC(hashSet);
	_tmf_params = [_tmf_params,"LOST_MSG",_lost_msg] call FFUNC(hashSet);
	_tmf_params = [_tmf_params,"SHARED_VAR",_shared_var] call FFUNC(hashSet);
	_tmf_params = [_tmf_params,"HOLD_TIME",_hold_time] call FFUNC(hashSet);
	_tmf_params = [_tmf_params,"OWNER_SIDE",_owner_side] call FFUNC(hashSet);
	// _tmf_params = [_tmf_params,"TAKER_SIDE",[GVAR(side_own)]] call FFUNC(hashSet);
	// _tmf_params = [_tmf_params,"TAKEN_CODE",""] call FFUNC(hashSet);
	// _tmf_params = [_tmf_params,"LOST_CODE",""] call FFUNC(hashSet);
	// _tmf_params = [_tmf_params,"SHARED_DIST",300] call FFUNC(hashSet);
	TRACE_1("TMF Params",_tmf_params);
	_obj = [_obj_params,"TAKEOVER",_tmf_params] call FFUNC(CreateMissionObject);
	TRACE_1("End",ARR_3(_obj, typeOf _obj, getPos _obj));
} else {
	ERROR("Invalid Parameters");
};
TMFPROFILERSTOP;
_obj